#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <vector>
int main() {
    int n;
    cin >> n;
    vector<int> v(n, 0);
    for (int i = 0; i < n; i++)
        cin >> v[i];
    vector<int> dp(n + 1, 0);
    int _max;
    dp[2] = min(v[0], v[1]);
    _max = max(v[0], v[1]);
    for (int i = 3; i <= n; i++)
    {

        if (v[i - 1] > dp[i - 1])
        {
            if (v[i - 1] >= _max)
            {
                dp[i] = _max;
            }
            else {
                dp[i] = v[i - 1];
            }
        }
        else {
            dp[i] = dp[i - 1];
        }
        _max = max(_max, v[i - 1]);
    }
    int q;
    cin >> q;
    while (q--)
    {
        int val;
        cin >> val;
        cout << dp[val] << endl;
    }
    return 0;
}
#include <iostream>
using namespace std;
#include <vector>
int n;
void func1(vector<vector<int>>& vv)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            swap(vv[i][j], vv[j][i]);
        }
    }
}
void func2(vector<vector<int>>& vv)
{
    for (int i = 0; i < n / 2; i++)
    {
        for (int j = 0; j < n; j++)
        {
            swap(vv[i][j], vv[n - i - 1][j]);
        }
    }
}

int main() {

    cin >> n;
    vector<vector<int>> vv(n, vector<int>(n, 0));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
            cin >> vv[i][j];
    }
    int q;
    cin >> q;
    while (q--)
    {
        int val;
        cin >> val;
        if (val == 1)
        {
            func1(vv);
        }
        else {
            func2(vv);
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
            cout << vv[i][j] << " ";
        cout << endl;
    }
    return 0;
}

#include <iostream>
using namespace std;
#include <vector>
int main() {
    int n, k;
    cin >> n >> k;
    vector<int> v(n + 1, 0);
    for (int i = 1; i <= n; i++)
        cin >> v[i];
    vector<vector<int>> dp(n + 1, vector<int>(k, -1));
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < k; j++) {

            int index;
            // if (v[i] % k == j) {
            //     dp[i][j] = v[i];
            //     if(dp[i-1][j]!=-1)
            //     {
            //         dp[i][j] = dp[i-1][j]+v[i];
            //     }
            //     // for (int m = 1; m * j <= k; m++) {
            //     //     if (dp[i - 1][m * j] != -1) {
            //     //         dp[i][j] = max(dp[i][j], dp[i - 1][m * j] + v[i]);
            //     //     }
            //     // }
            //     //continue;
            // } else {
            if (v[i] % k == j)
            {
                dp[i][j] = v[i];
            }
            index = ((k - (v[i] % k)) + j) % k;
            if (dp[i - 1][index] != -1) {
                dp[i][j] = dp[i - 1][index] + v[i];
            }
            //dp[i][j] = max(dp[i-1][j],dp[i][j]);
        //}

        //cout << dp[i][j] << " ";
        }
        //cout << endl;
    }
    if (dp[n][0] == -1)
        cout << -1;
    else
        cout << dp[n][0];
    return 0;
}